package jjn.round1;

import commons.TreeNode;

/**
 * @author Jjn
 * @since 2022-08-08 11:09
 */
public class LeetCode111_MinimumDepthOfBinaryTree {
    
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        int minDepth = Integer.MAX_VALUE;
        if (left == null && right == null) {
            return 1;
        }
        if (left != null) {
            int leftDepth = minDepth(left);
            minDepth = Math.min(leftDepth, minDepth);
        }
        if (right != null) {
            int rightDepth = minDepth(right);
            minDepth = Math.min(rightDepth, minDepth);
        }
        return minDepth + 1;
    }
}
